package net.csdn.business.notify.handler.action;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import net.csdn.business.notify.common.constant.CommonConstants;
import net.csdn.business.notify.common.enums.NotifyChannel;
import net.csdn.business.notify.common.enums.NotifyType;
import net.csdn.business.notify.common.model.query.NotifyUserSettingQuery;
import net.csdn.business.notify.common.model.vo.NotifyUserSettingVO;
import net.csdn.business.notify.common.pipeline.BusinessProcess;
import net.csdn.business.notify.common.pipeline.ProcessContext;
import net.csdn.business.notify.common.pipeline.domain.TaskInfo;
import net.csdn.business.notify.handler.service.INotifyUserSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @ClassName UserShieldAction
 * @Description 用户屏蔽消息逻辑：查询用户配置表
 * @Author zhangyl
 * @Date 2023/11/30 17:03
 */
@Slf4j
@Service
public class RemindShieldAction implements BusinessProcess<TaskInfo> {


    @Autowired
    private INotifyUserSettingService userSettingService;

    @Override
    public void process(ProcessContext<TaskInfo> context) {
        TaskInfo taskInfo = context.getProcessModel();
        Integer notifyType=taskInfo.getNotifyType();
        //私信类通知，则不检查用户配置的开关
        /*if(notifyType.equals(NotifyType.PRIVATE.getCode())){
            return;
        }*/
        List<String> receiversBefore=new ArrayList<>(taskInfo.getReceiver());
        Integer notifyChannel=taskInfo.getNotifyChannel();
        //String channelCode= NotifyChannel.ofCodeEn(notifyChannel);
        String targetType=taskInfo.getTargetType();
        String act=taskInfo.getAct();
        //查询系统预设配置,如果系统预设都没这个配置，证明这个配置用户也不可能存在，直接跳过。
        NotifyUserSettingVO systemSet=userSettingService.selectNotifyUserSetting(targetType,act,"-1");
        if (systemSet!=null){
            //获取用户配置
            //防止查询数据过多，分批查询
            Set<String> receiversAfter=new HashSet<>();
            List<List<String>> uidBathList= Lists.partition(receiversBefore, 200);
            for (List<String> userIdList:uidBathList){
                //批量查询用户信息配置信息
                List<String> batchUserIdAfter=new ArrayList<>();
                NotifyUserSettingQuery query=new NotifyUserSettingQuery();
                query.setAct(act);
                query.setTargetType(targetType);
                query.setUserIds(userIdList);
                List<NotifyUserSettingVO>  userSetting=userSettingService.batchQuery(query);
                if(userSetting!=null&&userSetting.size()>0){
                    userSetting.forEach(setting-> {
                        String userId=setting.getUserId();
                        //此处配置形式:{chanel编码：true或fasle}
                        JSONObject set=setting.getSetting();
                        if(set!=null){
                            JSONObject channelSet=set.getJSONObject(notifyChannel.toString());
                            Boolean isOpen=channelSet.getBoolean(CommonConstants.IS_OPEN);
                            //如果已开启，则加入本次批次处理的集合
                            if(isOpen){
                                batchUserIdAfter.add(userId);
                            }else{
                                log.info("notify shield by user setting:target_type:{},act:{},channel{},user：{}, ",targetType,act,notifyChannel,userId);
                            }
                            userIdList.remove(userId);
                        }
                    });
                }
                //用户自定义配置验证完毕之后，判断userIdList是否为空，不为空，证明还剩余用户走的是默认系统配置。
                if(userIdList.size()>0){
                    JSONObject set=systemSet.getSetting();
                    if(set!=null){
                        JSONObject channelSet=set.getJSONObject(notifyChannel.toString());
                        Boolean isOpen=channelSet.getBoolean(CommonConstants.IS_OPEN);
                        //如果已开启，则加入本次批次处理的集合
                        if(isOpen){
                            batchUserIdAfter.addAll(userIdList);
                        }else{
                            log.info("notify shield by system default:target_type:{},act:{},channel{}, user：{}, ",targetType,act, notifyChannel,JSON.toJSONString(userIdList));
                        }
                    }
                }
                receiversAfter.addAll(batchUserIdAfter);
                if(uidBathList.size()==0){
                    break;
                }
            }
            taskInfo.setReceiver(receiversAfter);
        }
    }
}
